/*
 *  glut_main.cpp
 *  SketchMenu
 *
 *  Created by George on 4/29/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */


#include <iostream>
#include "glut_main.h"

#ifdef WIN32
  #include <gl/glut.h>
#else
  #include <GLUT/glut.h>
#endif

#define P_WIDTH 500
#define P_HEIGHT 500

static float * color = new float[3];

static int currentMouseX = 0;
static int currentMouseY = 0;
static float tiltX;
static float tiltY;
static SketchMenu s;

void init(void){
	glClear (GL_COLOR_BUFFER_BIT);
  glEnable (GL_BLEND);
  glEnable (GL_POLYGON_SMOOTH);
  glDisable (GL_DEPTH_TEST);
  glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);
  glEnable (GL_LINE_SMOOTH);
   glEnable (GL_BLEND);
   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
   glLineWidth (1.5);
   glClearColor (0.0, 0.0, 0.0, 0.0);
  tiltX = 0.0f;
  tiltY = 0.0f;
	//glShadeModel(GL_FLAT);
}
void display(void){
	glClearColor(0.0f,0.0f,0.0f,0.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	s.draw();
  glFlush();
}
void reshape(int w, int h){
	/*glViewport(0,0,(GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-50.0,50.0,-50.0, 50.0,-1.0,1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();*/
}
void mouseMove(int x, int y){
	currentMouseX = x;
	currentMouseY = y;
}
void mouse(int button, int state, int x, int y){
  sketch_event e;
  e.loc.x = x;
  e.loc.y = P_HEIGHT-y;
  e.tilt.x = tiltX;
  e.tilt.y = tiltY;
	switch(button){
		case GLUT_LEFT_BUTTON:
			if (state == GLUT_DOWN){
        
			}
			if (state == GLUT_UP){
        
			}
			break;
		case GLUT_RIGHT_BUTTON:
			if (state == GLUT_DOWN){
        
			}
			if (state == GLUT_UP){
        
			}
			break;
		default:
			glutIdleFunc(NULL);
			break;
	}
  s.handle_sketch_event(e);
  glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y){
	switch(key){
		case '=':
      if(tiltX < 1)tiltX += .1f;
			break;
		case '-':
      if(tiltX > -1)tiltX -= .1f;
			break;
    case '[':
      if(tiltY > -1)tiltY -= .1f;
      break;
    case ']':
      if(tiltY < 1)tiltY += .1f;
      break;
	}
  printf("Tilt:%f,%f\n",tiltX,tiltY);
}

int main(int argc, char* argv[]){
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB| GLUT_ALPHA);
	glutInitWindowSize(P_WIDTH,P_HEIGHT);
  s.set_size(P_WIDTH,P_HEIGHT);
	glutInitWindowPosition(100,100);
	glutCreateWindow("George Lucchese - Assignment 1");
	init();
	glutDisplayFunc(display);
	glutMouseFunc(mouse);
	glutKeyboardFunc(keyboard);
	glutMotionFunc(mouseMove);
	glutReshapeFunc(reshape);
	
	glutMainLoop();
	return 0;
}
